﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Swift;
using Swift.Lib;

namespace CB.Logic
{
    /// <summary>
    /// 色块匹配游戏
    /// </summary>
    public class ColorGame : GameData<ColorBlock>
    {
        public List<List<ColorBlock>> TargetBlocks = new List<List<ColorBlock>>(); // 目标色块

        public override IGameData Init(int w, int h, int c, int srandSeed)
        {
            base.Init(w, h, c, srandSeed);

            var count = w * h - 1;
            if (count % c != 0)
            {
                throw new Exception($"cannot arrange {c} colors to {count} grids");
            }

            var num = count / c;
            var colors = new List<BColor>();
            for (int i = 0; i < c; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    colors.Add((BColor)i);
                }
            }
            colors.SwiftRandomSort(SRandom);

            colors.Add(BColor.None);

            // 完成时的形态
            for (int i = 0; i < h; i++)
            {
                var l = new List<ColorBlock>();
                for (int j = 0; j < w; j++)
                {
                    var b = new ColorBlock()
                    {
                        X = j,
                        Y = i,
                        BColor = colors[i * w + j]
                    };
                    l.Add(b);
                    if (b.BColor == BColor.None)
                    {
                        EmptyBlock = b;
                        b.IsEmpty = true;
                    }
                }
                Blocks.Add(l);
            }

            // 获取最终目标色块
            for (int i = 0; i < h - 2; i++)
            {
                var l = new List<ColorBlock>();
                for (int j = 0; j < w - 2; j++)
                {
                    l.Add(new ColorBlock()
                    {
                        X = i,
                        Y = j,
                        BColor = Blocks[i + 1][j + 1].BColor
                    });
                }
                TargetBlocks.Add(l);
            }

            // 使用移动方式随机打乱所有色块
            while (GameFinish)
            {
                RandomMove(SRandom, 50);
            }

            return this;
        }

        public override bool GameFinish
        {
            get
            {
                for (int i = 1; i < Height - 1; i++)
                {
                    for (int j = 1; j < Width - 1; j++)
                    {
                        if (!BlockMatch(j, i)) return false;
                    }
                }
                return true;
            }
        }

        /// <summary>
        /// 单色块判断, 传入的坐标是所有色块而非展示用的目标色块
        /// </summary>
        public override bool BlockMatch(int x, int y)
        {
            if (x == 0 || y == 0 || x == Width - 1 || y == Height - 1)
                return true;

            return Blocks[y][x].BColor == TargetBlocks[y - 1][x - 1].BColor;
        }
    }
}
